package biz

import (
	"fmt"
	"server/base"
	"strconv"
	"strings"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

type GameItem struct {
	No     int     `json:"no"`
	Name   string  `json:"name"`
	Size   float64 `json:"size"`
	Genre  string  `json:"genre"`
	Letter string  `json:"letter"`
	Added  bool    `json:"added"`
	Pinyin string  `json:"pinyin"`
}

var GameTable []*GameItem

func ReadExcel(filename string) []*GameItem {
	var out []*GameItem
	f, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return out
	}
	sheets := f.GetSheetList()
	no := 0
	for _, shname := range sheets {
		rows, err := f.GetRows(shname)
		base.Raise(err)
		for _, row := range rows[2:] {
			if len(row) <= 14 {
				continue
			}
			sizef, _ := strconv.ParseFloat(row[1], 64)
			name := row[2]
			genre := row[4]
			if sizef == 0 || name == "" {
				continue
			}
			py := base.PinyinString(name)
			letter := row[13]
			if letter == "" && len(py) > 0 {
				letter = strings.ToUpper(py[:1])
			}
			no += 1
			it := &GameItem{
				No:     no,
				Name:   name,
				Size:   sizef,
				Genre:  genre,
				Letter: letter,
				Pinyin: py,
			}
			out = append(out, it)
		}
	}
	GameTable = out
	return out
}
